package com.base.cn.platform.os.service.statistics.users;

import com.base.cn.platform.os.common.j2cache.CustomCacheChannel;
import com.base.cn.platform.os.common.j2cache.J2CacheRegion;
import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.utils.DateUtils;
import com.base.cn.platform.os.dao.statistics.users.ZStatisticsPayUserCountDao;
import com.base.cn.platform.os.entity.statistics.users.ZStatisticsPayUserCount;
import com.base.cn.platform.os.service.order.order.OrderBiz;
import com.base.cn.platform.os.service.user.user.CusUserBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.Map;

/**
 * 付费用户数据统计Biz
 *
 * @author s.li
 * @date 2019/05/05
 */
@Service
public class ZStatisticsPayUserCountBiz extends BaseBiz<ZStatisticsPayUserCount, ZStatisticsPayUserCountDao> {
    private static DecimalFormat decimalFormat = new DecimalFormat("#.##");
    @Autowired
    private OrderBiz orderBiz;
    @Autowired
    private CusUserBiz cusUserBiz;
    @Autowired
    private CustomCacheChannel customCacheChannel;

    /**
     * 查询付款用户概况数据
     * @param date 查询时间
     * @return ZStatisticsPayUserCount
     */
    public ZStatisticsPayUserCount queryPayUserCountData(Date date){
        String strTime = DateUtils.format(date,"yyyy-MM-dd");
        String cacheName = "pay_user_count_data_"+strTime.replaceAll("-","_");
        ZStatisticsPayUserCount payUserCount = (ZStatisticsPayUserCount)customCacheChannel.get(J2CacheRegion.THIRTY_MINUTE_REGION.getKey(),cacheName).getValue();
        if(payUserCount==null){
            payUserCount = this.findOne("countTime ='"+strTime+"'",null);
            if(payUserCount==null){//如果数据不存在，则查询数据
                payUserCount = this.getData(date,strTime);
                Date _date = new Date();
                String _strTime = DateUtils.format(_date,"yyyy-MM-dd");
                if(!strTime.equals(_strTime)){
                    payUserCount.setCountTime(date);
                    this.save(payUserCount);
                }
                customCacheChannel.set(J2CacheRegion.THIRTY_MINUTE_REGION.getKey(),cacheName,payUserCount);
            }
        }
        return payUserCount;
    }

    /**
     * 获取数据
     * @param date
     * @param strTime
     * @return
     */
    private ZStatisticsPayUserCount getData(Date date,String strTime){
        ZStatisticsPayUserCount payUserCount = orderBiz.queryPayUserCountData(strTime);
        if(payUserCount==null){
            payUserCount = new ZStatisticsPayUserCount();
        }
        //查询用户数量
        Map<String, BigDecimal> result = cusUserBiz.queryUserPayCountData(date);

        int userNum = result.get("userNum").intValue();
        int yesPayUserNum = result.get("yesPayUserNum").intValue();
        int notPayUserNum = result.get("notPayUserNum").intValue();

        //总付费转化率
        Float conversionRate = new Float(payUserCount.getPayUserNum()) / new Float(userNum);
        if(conversionRate.isNaN() || conversionRate.isInfinite()){
            conversionRate = 0.00f;
        }
        conversionRate = conversionRate * 100f;
        payUserCount.setConversionRate(Float.parseFloat(decimalFormat.format(conversionRate)));

        //新用户付费转化率
        Float newConversionRate = new Float(payUserCount.getNewPayUserNum()) / new Float(notPayUserNum);
        if(newConversionRate.isInfinite() || newConversionRate.isNaN()){
            newConversionRate = 0.00f;
        }
        newConversionRate = newConversionRate * 100f;
        payUserCount.setNewConversionRate(Float.parseFloat(decimalFormat.format(newConversionRate)));

        //老用户付费转化率
        Float oldConversionRate = new Float(payUserCount.getOldPayUserNum()) / new Float(yesPayUserNum);
        if(oldConversionRate.isInfinite() || oldConversionRate.isNaN()){
            oldConversionRate = 0.00f;
        }
        oldConversionRate = oldConversionRate * 100f;
        payUserCount.setOldConversionRate(Float.parseFloat(decimalFormat.format(oldConversionRate)));

        //总平均付费金额
        Double averagePrice = payUserCount.getPayPrice() / new Double(payUserCount.getPayUserNum());
        if(averagePrice.isNaN() || averagePrice.isInfinite()){
            averagePrice = 0.00d;
        }
        payUserCount.setAveragePrice(Double.parseDouble(decimalFormat.format(averagePrice)));

        //新用户付费平均金额
        Double newAveragePrice = payUserCount.getNewPayPrice() / new Double(payUserCount.getNewPayUserNum());
        if(newAveragePrice.isInfinite() || newAveragePrice.isNaN()){
            newAveragePrice = 0.00d;
        }
        payUserCount.setNewAveragePrice(Double.parseDouble(decimalFormat.format(newAveragePrice)));

        //老用户平均金额
        Double oldAveragePrice = payUserCount.getOldPayPrice() / new Double(payUserCount.getOldPayUserNum());
        if(oldAveragePrice.isNaN() || oldAveragePrice.isInfinite()){
            oldAveragePrice = 0.00d;
        }
        payUserCount.setOldAveragePrice(Double.parseDouble(decimalFormat.format(oldAveragePrice)));

        //新用户的占比
        Float newUserShare = new Float(payUserCount.getNewPayUserNum()) / new Float(payUserCount.getPayUserNum());
        if(newUserShare.isNaN() || newUserShare.isInfinite()){
            newUserShare = 0.00f;
        }
        newUserShare = newUserShare * 100f;
        payUserCount.setNewUserShare(Float.parseFloat(decimalFormat.format(newUserShare)));

        //老用户的占比
        Float oldUserShare = new Float(payUserCount.getOldPayUserNum()) / new Float(payUserCount.getPayUserNum());
        if(oldUserShare.isNaN() || oldUserShare.isInfinite()){
            oldUserShare = 0.00f;
        }
        oldUserShare = oldUserShare * 100f;
        payUserCount.setOldUserShare(Float.parseFloat(decimalFormat.format(oldUserShare)));

        return payUserCount;
    }
}
